package com.wh.controller;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.PropertyFilter;
import com.wh.beans.ConstVar;
import com.wh.beans.Enum.UserTypeEnum;
import com.wh.beans.PathVariable.LoginInfo;
import com.wh.beans.PathVariable.LoginParam;
import com.wh.beans.PathVariable.RegisterParam;
import com.wh.service.IExt.ISysUserBaseService;
import com.wh.service.impl.SysUserBaseService;
import com.legend.utils.*;
import org.apache.log4j.Logger;
import org.springframework.http.HttpEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
 * Created by ayakatsune on 14-6-13.
 */
@Controller
@RequestMapping("api")
public class WorkUserController {
    Logger log = Logger.getRootLogger();

    @RequestMapping("login")
    @ResponseBody
    public String login(@ModelAttribute LoginParam login,HttpServletResponse response) throws Exception {
        ReturnResult result = new ReturnResult();
        if (StringUtils.isEmpty(login.getUserName())) {
            result.setMsg(i18nUtils.getVal("200001001"));
        } else if (StringUtils.isEmpty(login.getPassword())) {
            result.setMsg(i18nUtils.getVal("200002001"));
        } else {
            if (StringUtils.isEmpty(login.getValidcode())) {
                result.setMsg(i18nUtils.getVal("200003002"));
            } else if (!login.getValidcode().equalsIgnoreCase(
                    String.valueOf(SessionUtils.getInstance().get(RandomValidateCode.RANDOMCODEKEY)))) {
                result.setMsg(i18nUtils.getVal("200003001"));
            } else {
                ISysUserBaseService service =SysUserBaseService.getInstance();
                result = service.userLogin(login,UserTypeEnum.WorkUser);
                if (result.getData() != null && result.getStatus() == Status.SUCCESS) {

                    String str = DESCoder.encrypt(JSONObject.toJSONString(result.getData()), DESCoder.KEY_STR);
                    log.debug(str);
                    CookieUtils.addCookie(response, ConstVar.WORK_USER_LOGIN_INFO, str);
                    result.setData("");
                }
            }
        }
        return JSONObject.toJSONString(result);
    }

    @RequestMapping(value = "register", method = RequestMethod.POST)
    @ResponseBody
    public String register(HttpServletRequest request,@ModelAttribute RegisterParam param) {
        ReturnResult result = new ReturnResult();
       // String data=Request.getData(request);
        //    if (data != null || data.length() < 30) {
              //  RegisterParam param = JSONObject.parseObject(data, RegisterParam.class);
                ISysUserBaseService ubs = SysUserBaseService.getInstance();
                result = ubs.register(param, UserTypeEnum.WorkUser);
         //   } else {
          //      result.setStatus(Status.ERROR);
          //      result.setMsg(i18nUtils.getVal("100007000"));
         //   }
        return JSONObject.toJSONString(result);
    }

}
